/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ordering.dao;

import com.ordering.dao.exceptions.IllegalOrphanException;
import com.ordering.dao.exceptions.NonexistentEntityException;
import com.ordering.pojo.TOrderHead;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import com.ordering.pojo.TOrderDetail;
import com.ordering.utils.ConfigIni;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author sulan
 */
public class TOrderHeadJpaController {

    public TOrderHeadJpaController() {
        emf = EntityManagerUtils.em;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TOrderHead TOrderHead) {
        if (TOrderHead.getTOrderDetailCollection() == null) {
            TOrderHead.setTOrderDetailCollection(new ArrayList<TOrderDetail>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<TOrderDetail> attachedTOrderDetailCollection = new ArrayList<TOrderDetail>();
            for (TOrderDetail TOrderDetailCollectionTOrderDetailToAttach : TOrderHead.getTOrderDetailCollection()) {
                TOrderDetailCollectionTOrderDetailToAttach = em.getReference(TOrderDetailCollectionTOrderDetailToAttach.getClass(), TOrderDetailCollectionTOrderDetailToAttach.getId());
                attachedTOrderDetailCollection.add(TOrderDetailCollectionTOrderDetailToAttach);
            }
            TOrderHead.setTOrderDetailCollection(attachedTOrderDetailCollection);
            em.persist(TOrderHead);
            for (TOrderDetail TOrderDetailCollectionTOrderDetail : TOrderHead.getTOrderDetailCollection()) {
                TOrderHead oldOrderHeadIdOfTOrderDetailCollectionTOrderDetail = TOrderDetailCollectionTOrderDetail.getOrderHeadId();
                TOrderDetailCollectionTOrderDetail.setOrderHeadId(TOrderHead);
                TOrderDetailCollectionTOrderDetail = em.merge(TOrderDetailCollectionTOrderDetail);
                if (oldOrderHeadIdOfTOrderDetailCollectionTOrderDetail != null) {
                    oldOrderHeadIdOfTOrderDetailCollectionTOrderDetail.getTOrderDetailCollection().remove(TOrderDetailCollectionTOrderDetail);
                    oldOrderHeadIdOfTOrderDetailCollectionTOrderDetail = em.merge(oldOrderHeadIdOfTOrderDetailCollectionTOrderDetail);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TOrderHead TOrderHead) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TOrderHead persistentTOrderHead = em.find(TOrderHead.class, TOrderHead.getId());
            Collection<TOrderDetail> TOrderDetailCollectionOld = persistentTOrderHead.getTOrderDetailCollection();
            Collection<TOrderDetail> TOrderDetailCollectionNew = TOrderHead.getTOrderDetailCollection();
            List<String> illegalOrphanMessages = null;
            for (TOrderDetail TOrderDetailCollectionOldTOrderDetail : TOrderDetailCollectionOld) {
                if (!TOrderDetailCollectionNew.contains(TOrderDetailCollectionOldTOrderDetail)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain TOrderDetail " + TOrderDetailCollectionOldTOrderDetail + " since its orderHeadId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<TOrderDetail> attachedTOrderDetailCollectionNew = new ArrayList<TOrderDetail>();
            for (TOrderDetail TOrderDetailCollectionNewTOrderDetailToAttach : TOrderDetailCollectionNew) {
                TOrderDetailCollectionNewTOrderDetailToAttach = em.getReference(TOrderDetailCollectionNewTOrderDetailToAttach.getClass(), TOrderDetailCollectionNewTOrderDetailToAttach.getId());
                attachedTOrderDetailCollectionNew.add(TOrderDetailCollectionNewTOrderDetailToAttach);
            }
            TOrderDetailCollectionNew = attachedTOrderDetailCollectionNew;
            TOrderHead.setTOrderDetailCollection(TOrderDetailCollectionNew);
            TOrderHead = em.merge(TOrderHead);
            for (TOrderDetail TOrderDetailCollectionNewTOrderDetail : TOrderDetailCollectionNew) {
                if (!TOrderDetailCollectionOld.contains(TOrderDetailCollectionNewTOrderDetail)) {
                    TOrderHead oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail = TOrderDetailCollectionNewTOrderDetail.getOrderHeadId();
                    TOrderDetailCollectionNewTOrderDetail.setOrderHeadId(TOrderHead);
                    TOrderDetailCollectionNewTOrderDetail = em.merge(TOrderDetailCollectionNewTOrderDetail);
                    if (oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail != null && !oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail.equals(TOrderHead)) {
                        oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail.getTOrderDetailCollection().remove(TOrderDetailCollectionNewTOrderDetail);
                        oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail = em.merge(oldOrderHeadIdOfTOrderDetailCollectionNewTOrderDetail);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = TOrderHead.getId();
                if (findTOrderHead(id) == null) {
                    throw new NonexistentEntityException("The tOrderHead with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TOrderHead TOrderHead;
            try {
                TOrderHead = em.getReference(TOrderHead.class, id);
                TOrderHead.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The TOrderHead with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<TOrderDetail> TOrderDetailCollectionOrphanCheck = TOrderHead.getTOrderDetailCollection();
            for (TOrderDetail TOrderDetailCollectionOrphanCheckTOrderDetail : TOrderDetailCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TOrderHead (" + TOrderHead + ") cannot be destroyed since the TOrderDetail " + TOrderDetailCollectionOrphanCheckTOrderDetail + " in its TOrderDetailCollection field has a non-nullable orderHeadId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(TOrderHead);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TOrderHead> findTOrderHeadEntities() {
        return findTOrderHeadEntities(true, -1, -1);
    }

    public List<TOrderHead> findTOrderHeadEntities(int maxResults, int firstResult) {
        return findTOrderHeadEntities(false, maxResults, firstResult);
    }

    private List<TOrderHead> findTOrderHeadEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TOrderHead as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }


    public List<TOrderHead> findTOrderHeadEntitiesBySQL(String sql) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery(sql);
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public TOrderHead findTOrderHead(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TOrderHead.class, id);
        } finally {
            em.close();
        }
    }

    public int getTOrderHeadCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from TOrderHead as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
